<?php

class Advertise_m extends CI_Model{
	function __construct(){
		parent::__construct();
		$this->load->model('admin_m');
		
	} // end function construct..
	function get_data(){
		$res = $this->db->get('advertisemens');
		return ($res->num_rows() > 0) ? $res->result():false;
	}// end get data function..
	function get_adds_location($location){
		$loc = ($location == 'index') ? "":$location;
		
		$this->db->where('location',$loc);
		$res = $this->db->get('advertisemens');
		
		$data[] = array();
		
		if($res->num_rows() > 0){
			
			return $res->result();
		}
		else{
			return false;
		}
		
		//return ($res->num_rows() > 0) ? $res->result():false;
		
	}// get_adds_location function..
	function save_ads(){
		
		$admin = $this->admin_m->get_admin_info($this->session->userdata('admin_id'));
		
		// uploading the banner ads file.. 
		$fileext = $_FILES['banner_img']['name'];
		$ext = substr(strrchr($fileext, '.'), 1); 
		$filename = $admin->id.'_'.str_replace(' ', '_',$this->input->post('company_name')).'.'.$ext;
		
		$config['upload_path'] = './images/adds';
		$config['allowed_types'] = 'jpg|png';
		$config['max_size']	= '500';
		$config['max_width']  = '620';
		$config['max_height']  = '300';
		$config['file_name'] = $filename;
		
		$this->upload->initialize($config);
		// loading the configuration.. 

		if ( ! $this->upload->do_upload('banner_img'))
		{
			$error = array('error' => $this->upload->display_errors());
			$filename = "default.png";
			
		}

		$data = array(
			'admin_id' => $admin->id,
			'client_id' => '', // this will only add if client will add a ads using on there login..
			'banner_img' => "images/adds/".$filename, // the uploaded file..
			'caption' => $this->input->post('caption'),
			'company_name' => $this->input->post('company_name'),
			'location' => $this->input->post('location'),
			'link' => $this->input->post('link'),
		);
		
		$res = $this->db->insert('advertisemens',$data);
		
		return ($res) ? true:false;
		
	}
}// end advertise model..